Backspace String Compare
Question
None
Example 1
Input: S = "ab#c"
T = "ad#c"
Output: true
Solution
- ▭
- ▯
all//Backspace String Compare.py
def backspaceStringCompare(S, T):
i = len(S) - 1
j = len(T) - 1
while True:
backS = 0
backT = 0
while i >= 0 and (backS or S[i] == "#"):
backS += 1 if S[i] == "#" else -1
i -= 1
while j >= 0 and (backT or T[j] == "#"):
backT += 1 if T[j] == "#" else -1
j -= 1
if not (i >= 0 and j >= 0 and S[i] == T[j]):
return i == -1 and j == -1
i -= 1
j -= 1
S = "ab##"
T = "c#d#"
print(backspaceStringCompare(S, T)) # True
all//Backspace String Compare.py
def backspaceStringCompare(S, T):
i = len(S) - 1
j = len(T) - 1
while True:
backS = 0
backT = 0
while i >= 0 and (backS or S[i] == "#"):
backS += 1 if S[i] == "#" else -1
i -= 1
while j >= 0 and (backT or T[j] == "#"):
backT += 1 if T[j] == "#" else -1
j -= 1
if not (i >= 0 and j >= 0 and S[i] == T[j]):
return i == -1 and j == -1
i -= 1
j -= 1
S = "ab##"
T = "c#d#"
print(backspaceStringCompare(S, T)) # True